package com.yehun.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 更换数据源
 * Created by Genius on 2017/3/27 0027.
 */
public class MybatisDataSourceSwitch {

    private static final Logger logger = LoggerFactory.getLogger(MybatisDataSourceSwitch.class);

    private static final ThreadLocal<MybatisDataSourceType> contextHolder = ThreadLocal.withInitial(() -> MybatisDataSourceType.MASTER);

    /**
     * 切换回默认数据源
     */
    public static void setDefault() {
        logger.info("切换默认数据源");
        contextHolder.set(MybatisDataSourceType.MASTER);
    }

    /**
     * 切换数据源
     * @param dataSourceType 数据源
     */
    public static void set(MybatisDataSourceType dataSourceType) {
        logger.info("切换数据源 => " + dataSourceType);
        contextHolder.set(dataSourceType);

    }

    /**
     * 获取当前数据源
     * @return 数据源
     */
    public static MybatisDataSourceType get() {
        //return Enum.valueOf(MybatisDataSourceType.class, contextHolder.get());
        return contextHolder.get();
    }

}
